<?php
class parser
{
# Parse RambleCode
    public function parse($content)
    {
        global $config;
        $atags = "<".implode("><",explode(" ",$config['parse']['tags'])).">";
        $content = strip_tags($content,$atags);
        $tag = array (
                       'i' => '/\[i\]((.|\n)*?)\[\/i\]/',
                       'b' => '/\[b\]((.|\n)*?)\[\/b\]/',
                       'u' => '/\[u\]((.|\n)*?)\[\/u\]/',
                       'url' => '/\[url\]((.|\n)*?)\[\/url\]/',
                       'url2' => '/\[url=([a-zA-Z\:\.\/\-_]+)\\]((.|\n)*?)\[\/url\]/',
                     );
        $code = array (
                       'i' => '<i>$1</i>',
                       'b' => '<b>$1</b>',
                       'u' => '<u>$1</u>',
                       'url' => '<a href="$1">$1</a>',
                       'url2' => '/<a href="$1">$2</a>',
                     );
        $content = preg_replace($tag,$code,$content);
        $content = preg_replace_callback('/\[topic\]([\d]+)\[\/topic\]/',"parser::topic",$content);
        $content = preg_replace_callback('/\[user\]([a-zA-Z0-9\:\.\/\-_]+)\[\/user\]/','parser::user',$content);
        $content = preg_replace_callback('/\[post\]([\d]+)\[\/post\]/',"parser::post",$content);
        $content = preg_replace_callback('/\[topic=([\d]+)\]((.|\n)*?)\[\/topic\]/',"parser::topic2",$content);
        $content = preg_replace_callback('/\[user=([a-zA-Z0-9\:\.\/\-_]+)\]((.|\n)*?)\[\/user\]/',"parser::user2",$content);
        $content = preg_replace_callback('/\[post=([\d]+)\](.*?)\[\/post\]/',"parser::post2",$content);
        #$content = preg_replace_callback('/(\n*)\[whisper=([a-zA-Z0-9\:\.\/\-_\;]+)\](.*?)\[\/whisper\]/',"whisper",$content);
        return str_replace("\n","<br />",stripslashes($content));
    }
    # Magic private static functions
    private static function topic($matches)
    {
        global $config;
        $tname = ($r = api::get("topic","title",$matches[1])) ? $r : "No Topic";
        return "<a href=\"topic/view/{$matches[1]}\" title=\"$tname\">$tname</a>";
    }
    private static function topic2($matches)
    {
        global $config;
        $tname = api::get("topic","title",$matches[1]);
        if($tname) return "<a href=\"topic/view/{$matches[1]}\" title=\"$tname\">{$matches[2]}</a>";
        else return $matches[2];
    }
    private static function user($matches)
    {
        global $config;
        if(preg_match("/^(\d+)$/",$matches[1]))
        {
            $uid = $matches[1];
            $uname = api::get("user","name",$uid);
        }
        else
        {
            $uname = $matches[1];
            $uid = api::get("user","id",array("name" => $uname));
        }
        if(!$uid || !$uname) return "No User";
        else return "<a href=\"user/view/$uid/dims=350x450\" class=\"dialog\" title=\"$uname\">$uname</a>";
    }
    private static function user2($matches)
    {
        global $config;
        $uid = $matches[1];
        
        if(preg_match("/^(\d+)$/",$matches[1]))
        {
            $uid = $matches[1];
            $uname = api::get("user","name",$uid);
        }
        else
        {
            $uname = $matches[1];
            $uid = api::get("user","id",array("name" => $uname));
        }
        if($uname) return "<a href=\"user/view/$uid/dims=350x450\" class=\"dialog\" title=\"$uname\">{$matches[2]}</a>";
        else return "No User";
    }
    private static function post($matches)
    {
        global $config;
        $pname = ($r = api::get("post","title",$matches[1])) ? $r : "#{$matches[1]}";
        return "<a href=\"post.php?m=view;{$matches[1]}&amp;width=650&amp;height=275\" class=\"dialog\">$pname</a>";
    }
    private static function post2($matches)
    {
        global $config;
        $pname = api::get("post","title",$matches[1]);
        if($pname) return "<a href=\"post.php?m=view;{$matches[1]}&amp;width=650&amp;height=275\" class=\"dialog\" title=\"$pname\">{$matches[2]}</a>";
        else return "No Post";
    }
    private static function whisper($matches)
    {
        # Recode later
    }
}
?>